package zhr.chapter10;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Example10_3 {
    public static void main(String[] args) {
        File helloFile=new File("D://","HelloWorld.txt");
        try {
            //创建文件
            boolean isCreated=helloFile.createNewFile();
            if(isCreated||helloFile.exists()){
                //以字符流方式写入文件heeloFile
                String content[]={
                        "好雨知时节,当春乃发生.\n","随风潜入夜,润物细无声.\n",
                        "野径云俱黑,江船火独明.\n","晓看红湿处,花重锦官城.\n"
                };
                FileWriter write=new FileWriter(helloFile);
                BufferedWriter bufferedWriter=new BufferedWriter(write);
                for (String str:content){
                    bufferedWriter.write(str);
                    bufferedWriter.newLine();
                }
                bufferedWriter.close();
                write.close();
                //以字符流读取文件helloFile
                //FileReader reader=new FileReader(helloFile);
                InputStreamReader reader=new InputStreamReader(new FileInputStream(helloFile),"GBK");
                BufferedReader bufferedReader=new BufferedReader(reader);
                String s;
                while ((s=bufferedReader.readLine())!=null){
                    System.out.println(s);
                }
                bufferedReader.close();
                reader.close();

            }
        } catch (IOException e) {
            Logger.getLogger(Example10_3.class.getName()).log(Level.SEVERE,null,e);
        }
    }
}
